package com.atguigu.edu.controller.front;

import com.atguigu.utils.R;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.vo.ChapterVideoVo;
import com.atguigu.edu.entity.vo.front.CourseFrontVo;
import com.atguigu.edu.entity.vo.front.CourseWebVo;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@Api(description = "(热门课程) 前台端口-8001")
@RequestMapping("/edu/front")
public class CourseFrontController {

    @Autowired
    CourseService courseService;

    @ApiOperation("前台前端课程分页查询/可带条件")
    @PostMapping("/getCourse/{current}/{limit}")
    public R getCourseByLimit(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CourseFrontVo courseFrontVo) {


        QueryWrapper<Course> wrapper = null;
        if (courseFrontVo != null) {
            wrapper = new QueryWrapper<>();
//           查询规则
            if (!StringUtils.isEmpty(courseFrontVo.getSubjectOneId())) {
                wrapper.eq("subject_parent_id", courseFrontVo.getSubjectOneId());
            }
            if (!StringUtils.isEmpty(courseFrontVo.getSubjectTwoId())) {
                wrapper.eq("subject_id", courseFrontVo.getSubjectTwoId());
            }
//            排序规则
            if (!StringUtils.isEmpty(courseFrontVo.getIsNew())) {
                wrapper.orderByDesc("gmt_create");
            } else if (!StringUtils.isEmpty(courseFrontVo.getViewCount())) {
                wrapper.orderByDesc("view_count");
            } else if (!StringUtils.isEmpty(courseFrontVo.getPrice())) {
                wrapper.orderByAsc("price");
            }

        }
        Page<Course> page = new Page<>(current, limit);

        IPage<Course> iPage = courseService.page(page, wrapper);

        List<Course> records = iPage.getRecords();

        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();
        long total = iPage.getTotal();
        long current1 = iPage.getCurrent();
        long pages = iPage.getPages();
        long size = iPage.getSize();
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("records", records);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        map.put("current1", current1);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        return R.ok().data("data", map);
    }


    @Autowired
    ChapterService chapterService;

    /**
     * 课程详情的方法
     *
     * @param courseId
     * @return
     */
    @ApiOperation("获取课程信息+课程章节小节")
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVideoVo> chapterVideoList = chapterService.findChapterVideoById(courseId);
        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList);
    }


}
